﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.IO;

namespace mamnontuoitho7.backend
{
    public partial class ArticleEdit : System.Web.UI.Page
    {
        private ArticleType _articleType = new ArticleType();
        private Article _article = new Article();
        private Member _mem = new Member();
        protected DataTable _dt;
        protected string _productID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.QueryString["ProductID"] != null)
                    {
                        _productID = Request.QueryString["ProductID"].ToString();
                        hid_ArticleID.Value = _productID;
                    }

                    if (Request.Form["hidProductID"] != null)
                    {
                        _productID = Request.Form["hidProductID"].ToString();
                        hid_ArticleID.Value = _productID;
                    }

                    if (_productID == "")
                    {
                        _productID = hid_ArticleID.Value;
                    }

                    _dt = _article.GetArticleByIDAdmin(_productID);

                    if (!IsPostBack)
                    {
                        // load drop down list article type
                        DataTable dtArticleType = _articleType.GetAllArticleType2();
                        ddlArticleType.DataSource = dtArticleType;
                        ddlArticleType.DataTextField = "ArticleTypeName";
                        ddlArticleType.DataValueField = "ArticleTypeID";
                        ddlArticleType.DataBind();
                        ddlArticleType.SelectedValue = _dt.Rows[0]["ArticleTypeID"].ToString();

                        txtProductID.Text = _productID;
                        txtTitleName.Text = _dt.Rows[0]["Title"].ToString();
                        txtDescription.Text = _dt.Rows[0]["Introduce"].ToString();
                        ckeContent.Text = _dt.Rows[0]["Content"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnArticleUpdate_Click(object sender, EventArgs e)
        {
            try
            {
            _productID = hid_ArticleID.Value;
            bool checkArticleUpdate = _article.ArticleUpdate(_productID, ddlArticleType.SelectedValue, txtTitleName.Text, txtDescription.Text, ckeContent.Text);
                if (checkArticleUpdate)
                {
                    if (!string.IsNullOrEmpty(fulPhoto.FileName))
                    {
                        if (fulPhoto.FileBytes.Count() / 1024 > 6144)
                        {
                            String script = "<script language=JavaScript>alert('Dung lượng hình ảnh phải dưới 6mb')</script>";
                            Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                        }

                        string localfile = Request.PhysicalApplicationPath + "images/article/" + _dt.Rows[0]["Photo"].ToString();
                        string localfilethumb = Request.PhysicalApplicationPath + "images/article/thumbnail/" + _dt.Rows[0]["Photo"].ToString();
                        if (File.Exists(localfile))
                        {
                            File.Delete(localfile);
                            File.Delete(localfilethumb);
                        }

                        string fullname = fulPhoto.FileName;
                        string nameString = fullname.Substring(0, fullname.LastIndexOf('.'));

                        string fileExtension = fullname.Substring(fullname.LastIndexOf('.') + 1, fullname.Length - fullname.LastIndexOf('.') - 1).ToLower();

                        if (fileExtension == "jpg" || fileExtension == "gif" || fileExtension == "jpeg" || fileExtension == "png" || fileExtension == "bmp" || fileExtension == "tiff")
                        {
                            string FilePath = Request.PhysicalApplicationPath + "images/article/";
                            string code = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                            string name = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                            ThumbnailClass thumb = new ThumbnailClass();
                            String filename = thumb.UploadImage(fulPhoto, FilePath, code, name, 150, true, true);
                            _article.ArticlePhotoUpdate(_productID, filename);
                        }
                        else
                        {
                            String script = "<script language=JavaScript>alert('Chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff')</script>";
                            Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                        }
                    }
                    String script1 = "<script language=JavaScript>alert('Cập nhật thành công'); window.location = '../backend/ArticleEdit.aspx?ProductID=" + _productID+"';</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script1);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }

        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                _productID = hid_ArticleID.Value;
                DataTable dtArticleType = _articleType.GetAllArticleType2();
                ddlArticleType.DataSource = dtArticleType;
                ddlArticleType.DataTextField = "ArticleTypeName";
                ddlArticleType.DataValueField = "ArticleTypeID";
                ddlArticleType.DataBind();
                ddlArticleType.SelectedValue = _dt.Rows[0]["ArticleTypeID"].ToString();

                txtProductID.Text = _productID;
                txtTitleName.Text = _dt.Rows[0]["Title"].ToString();
                txtDescription.Text = _dt.Rows[0]["Introduce"].ToString();
                ckeContent.Text = _dt.Rows[0]["Content"].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }
    }
}